package com.china08.yxyapi.repository.db;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;

public interface YxyLessonsSchoolLessonRepository extends JpaRepository<YxyLessonsSchoolLesson, String> {

    YxyLessonsSchoolLesson findById(String id);

    Integer countByCatalogId1(String id1);

    Integer countByCatalogId2(String id2);

    List<YxyLessonsSchoolLesson> findByCatalogId2(String idTwo);

    Integer countByStatusAndSchoolId(Integer status, String schoolId);

    Integer countBySchoolId(String schoolId);

    YxyLessonsSchoolLesson findTop1BySchoolIdOrderByOrderNumDesc(String schoolId);

    // 查询草稿
    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.schoolId=:schoolId and t1.status = :status order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndStatus(@Param("schoolId") String schoolId, @Param("status") Integer status, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.schoolId=:schoolId and t1.status = :status and (t1.catalogId1 = :catalogId or t1.catalogId2 = :catalogId) and t1.name like CONCAT('%',:keyName,'%') order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndStatusAndCategoryIdAndNameLike(@Param("schoolId") String schoolId, @Param("status") Integer status, @Param("catalogId") String catalogId, @Param("keyName") String keyName, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.schoolId=:schoolId and t1.status = :status and t1.name like CONCAT('%',:keyName,'%') order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndStatusAndNameLike(@Param("schoolId") String schoolId, @Param("status") Integer status, @Param("keyName") String keyName, Pageable pageable);

    Integer countByCatalogId1AndCatalogId2(String id1, String id2);

    // 查询上架-下架
    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.status=2 and t1.schoolId=:schoolId and t1.isVisible = :isVisible order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndIsVisible(@Param("schoolId") String schoolId, @Param("isVisible") Integer isVisible, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.status=2 and t1.schoolId=:schoolId and t1.isVisible = :isVisible and (t1.catalogId1 = :catalogId or t1.catalogId2 = :catalogId ) and t1.name like CONCAT('%',:keyName,'%') order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndIsVisibleAndCategoryIdAndNameLike(@Param("schoolId") String schoolId, @Param("isVisible") Integer isVisible, @Param("catalogId") String catalogId, @Param("keyName") String keyName, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.status=2 and t1.schoolId=:schoolId and t1.isVisible = :isVisible and t1.name like CONCAT('%',:keyName,'%') order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndIsVisibleAndNameLike(@Param("schoolId") String schoolId, @Param("isVisible") Integer isVisible, @Param("keyName") String keyName, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.status >=0 and t1.schoolId=:schoolId order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findAllBySchoolId(@Param("schoolId") String schoolId, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.schoolId=:schoolId and t1.status >=0 and t1.name like CONCAT('%',:keyName,'%') order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndNameLike(@Param("schoolId") String schoolId, @Param("keyName") String keyName, Pageable pageable);

    @Query(value = " select t1 from YxyLessonsSchoolLesson t1 where t1.schoolId=:schoolId and t1.status >=0 and (t1.catalogId1 = :catalogId or t1.catalogId2 = :catalogId) and t1.name like CONCAT('%',:keyName,'%') order by t1.createdDate desc")
    Page<YxyLessonsSchoolLesson> findBySchoolIdAndCategoryIdAndNameLike(@Param("schoolId") String schoolId, @Param("catalogId") String catalogId, @Param("keyName") String keyName, Pageable pageable);

    Page<YxyLessonsSchoolLesson> findByNameContaining(String name, Pageable pageable);

    List<YxyLessonsSchoolLesson> findTop2ByAuthor(String aaName);

    List<YxyLessonsSchoolLesson> findTop2ByCatalogId2(String catalogId2);

    List<YxyLessonsSchoolLesson> findTop2ByAuthorAndStatusAndIsVisible(String aaName, int i, int j);

    List<YxyLessonsSchoolLesson> findByCatalogId2AndStatusAndIsVisible(String catalogId2, int i, int j);

    Integer countByCatalogId2AndStatusNot(String id2, int i);

    Integer countByCatalogId1AndStatusNot(String id1, int i);

    YxyLessonsSchoolLesson findBySchoolIdAndName(String schoolId, String name);

    List<YxyLessonsSchoolLesson> findTop2ByAuthorAndStatusAndIsVisibleOrderByVvDesc(String aaName, int i, int j);

    List<YxyLessonsSchoolLesson> findByCatalogId2AndStatusAndIsVisibleOrderByVvDesc(String catalogId2, int i, int j);

    YxyLessonsSchoolLesson findByIdAndSchoolIdAndVip(String string, String schoolId, int i);

    List<YxyLessonsSchoolLesson> findTop2ByCatalogId2AndStatusAndIsVisibleOrderByVvDesc(String catalogId2, int i, int j);

    Integer countByCatalogId1AndStatusIn(String id1, List<Integer> statusList);

    Integer countByCatalogId2AndStatusIn(String id2, List<Integer> statusList);
}
